<template>
  <div>
    <!-- 搜索提示列表 -->
    <van-list :error.sync="error" error-text="请求失败，点击重新加载" v-model="loading" :finished="finished" finished-text="没有更多了"
      @load="onLoad">
      <van-cell v-for="item in searchInfoList" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchRusults } from '@/api/search/index.js'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      // 搜索结果的传参
      searchInfo: {
        page: 1,
        per_page: 10,
        q: ''
      },
      searchInfoList: [],
      error: false
    }
  },
  methods: {
    // 发起获取结果数据请求
    async onLoad () {
      // 异步更新数据
      this.searchInfo.q = this.value
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      try {
        const { data: res } = await getSearchRusults(this.searchInfo)
        // console.log(res)
        // 新获取的存入到searchInfoList
        // if (Math.random() > 0.5) {
        //   console.lg(1212)
        // }
        this.searchInfoList.push(...res.data.results)
        // 判断存储的数据的长度和total0————count长度相等
        if (this.searchInfoList.length === res.data.total_count) {
          this.finished = true
        }
        // 继续触发加载时间
        this.loading = false
        this.searchInfo.page++
        // this.finished = true
      } catch (err) {
        this.loading = false
        this.error = true
      }
      // 加载状态结束
      // this.loading = false

      // 数据全部加载完成

      // this.finished = true
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
